from wtforms import Form, StringField
from wtforms.validators import DataRequired, ValidationError
from model.models import User


class RegisterForm(Form):
    # 账号和密码非空
    account = StringField(validators=[DataRequired()])
    password = StringField(validators=[DataRequired()])

    # 验证用户是否存在
    def validate_account(self, value):
        if User.query.filter(User.account == value.data).first():
            raise ValidationError("账号已存在")


# class LoginForm(Form):
#     userName = StringField(validators=[DataRequired()])
#     password = StringField(validators=[DataRequired()])
#
#     def validate(self):
#         # 调用父类validate 去验证输入的userName, password是否合法
#         super().validate()
#         # 如果输入不合法 self.errors就不为None， 就会为真，直接返回False 验证失败
#         if self.errors:
#             return False
#         user = User.query.filter_by(email=self.userName.data).first()
#         # 如果用户存在 并且密码验证通过
#         if user and check_password_hash(user.password, self.password.data):
#             return user
#         else:
#             raise ValidationError("验证失败！")
